package com.itheima.controller;

import com.itheima.pojo.Dept;
import com.itheima.pojo.Result;
import com.itheima.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 部门管理Controller
 */
@RestController
@Slf4j
@RequestMapping("depts")
public class DeptController {

    //注入Service对象
    @Autowired
    private DeptService deptService;

    //查询所有部门
    @GetMapping
    public Result list() {
        //1. 调用业务层
        List<Dept> list = deptService.list();
        log.info("查询所有部门数据 {}", list);
        //2. 响应数据
        return Result.success(list);
    }


    //根据主键删除部门
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable Integer id) {
        log.info("删除部门：{}", id);
        deptService.deleteById(id);
        return Result.success();
    }

    //添加部门信息
    @PostMapping
    public Result add(@RequestBody Dept dept){
        log.info("添加部门：{}", dept);
        deptService.add(dept);
        return Result.success();
    }

}
